package com.tmy.service.Impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tmy.Dto.Result;
import com.tmy.entity.*;
import com.tmy.mapper.*;
import com.tmy.service.CommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private RoomInfoMapper roomInfoMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    /**
     * 添加评论
     */
    @Override
    public Result addComment(Comment comment){
        Result result = new Result();
        try {
            comment.setCreateTime(new Date());
            int res = commentMapper.insert(comment);
            if (res==1){
                LambdaQueryWrapper<Comment> lqw =new LambdaQueryWrapper<>();
                lqw.eq(Comment::getRoomId,comment.getRoomId());
                List<Comment> commentList =commentMapper.selectList(lqw);
                RoomInfo roomInfo = roomInfoMapper.selectById(comment.getRoomId());
                if (commentList.isEmpty()){
                    DecimalFormat df = new DecimalFormat("#.0");
                    String formattedNumber = df.format(comment.getScore()/commentList.size());
                    roomInfo.setScore(formattedNumber);
                    int grade = (int) ((comment.getScore()/commentList.size())/2);
                    roomInfo.setGrade(grade);
                    roomInfo.setReview(1);
                }else {
                    Double tatolScore = 0.0;
                    DecimalFormat df = new DecimalFormat("#.0");
                    for (Comment item:commentList){
                        tatolScore=tatolScore+item.getScore();
                    }
                    String formattedNumber = df.format(tatolScore/commentList.size());
                    roomInfo.setScore(formattedNumber);
                    int grade = (int) ((tatolScore/commentList.size())/2);
                    roomInfo.setGrade(grade);
                    roomInfo.setReview(commentList.size());
                }
               roomInfoMapper.updateById(roomInfo);
                result.setMsg("评论成功");
                result.setCode(200);
            }else {
                result.setMsg("评论失败");
                result.setCode(400);
            }
        }catch (Exception err){
            result.setMsg(err.getCause());
            result.setCode(500);
        }
        return result;
    }

    /**
     * 查询评论
     */
    @Override
    public Result queryComment(Comment comment){
        Result result = new Result();
        try {
            LambdaQueryWrapper<Comment> lqw =new LambdaQueryWrapper<>();
            lqw.eq(Comment::getHotelId,comment.getHotelId());
            List<Comment> commentList = commentMapper.selectList(lqw);
            for (Comment item:commentList){
                LambdaQueryWrapper<RoomInfo> lqwRoom = new LambdaQueryWrapper<>();
                lqwRoom.eq(RoomInfo::getId,item.getHotelId());
                List<RoomInfo> roomInfoList = roomInfoMapper.selectList(lqwRoom);
                item.setRoomInfo(JSONObject.from(roomInfoList.get(0)));
                LambdaQueryWrapper<UserInfo> lqwUser = new LambdaQueryWrapper<>();
                lqwUser.eq(UserInfo::getId,item.getUserId());
                List<UserInfo>userList = userInfoMapper.selectList(lqwUser);
                item.setUserInfo(JSONObject.from(userList.get(0)));
            }
            result.setData(commentList);
            result.setMsg("评论查询成功");
            result.setCode(200);
        }catch (Exception err){
            result.setMsg(err.getCause());
            result.setCode(500);
        }
        return result;
    }
}
